package com.git.smp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties.Jwt;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.cache.SmpUserCache;
import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpUserMapper;
import com.git.smp.dao.SmpUserRoleMapper;
import com.git.smp.entity.SmpUser;
import com.git.smp.entity.SmpUserExtend;
import com.git.smp.entity.SmpUserRole;
import com.git.smp.utils.JWTUtil;
import com.git.smp.utils.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/user")
public class SmpUserController {

	Logger logger = LoggerFactory.getLogger(SmpUserController.class);

	@Autowired
	SmpUserMapper smpUserMapper;
	
	@Autowired
	SmpUserRoleMapper smpUserRoleMapper;
	

	@Value("${jwt.password}")
	private String jwtPassword;
	
	@Value("${jwt.version}")
	private String version;
	

	@Autowired
	private HttpServletRequest request;
	
	
	@RequestMapping("/resetPasswd")
	public ResponseMessage<?> resetPasswd(@RequestBody SmpUser smpUser){
		ResponseMessage<?> responseMessage = new ResponseMessage();
		responseMessage.setCode("0000");
		responseMessage.setMessage("密码重置成功！");
		
		
		smpUser.setUserPasswd("670b14728ad9902aecba32e22fa4f6bd");
		smpUserMapper.updateByPrimaryKey(smpUser);
		return responseMessage;
	}
	
	@RequestMapping("/updatePasswd")
	public ResponseMessage<?> updatePasswd(@RequestParam("oldpasswd") String oldpasswd,@RequestParam("newpasswd") String newpasswd){
		ResponseMessage<?> responseMessage = new ResponseMessage();
		responseMessage.setCode("0000");
		responseMessage.setMessage("密码重置成功！");
		
		 String applyUser = request.getHeader("userName");
		 
		 SmpUser smpUser = smpUserMapper.selectByPrimaryKey(applyUser);
		 
		 if(!oldpasswd.equals(smpUser.getUserPasswd())){
			 responseMessage.setCode("0001");
			responseMessage.setMessage("原密码不正确");
		 }else {
			 smpUser.setUserPasswd(newpasswd);
			 smpUserMapper.updateByPrimaryKey(smpUser);
		 }
		return responseMessage;
	}
	
	
	
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectAll")
	public ResponseMessage<List<SmpUser>> selectAll() {
		ResponseMessage<List<SmpUser>> responseMessage = new ResponseMessage<List<SmpUser>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		List<SmpUser> listSmpUser = SmpUserCache.getSmpUserList();
		if (listSmpUser == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		responseMessage.setExtend(listSmpUser);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectByRoleId")
	public ResponseMessage<List<SmpUser>> selectByRoleId(@RequestParam String roleId) {
		ResponseMessage<List<SmpUser>> responseMessage = new ResponseMessage<List<SmpUser>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		List<SmpUser> listSmpUser = smpUserMapper.selectByRoleId(roleId);
		if (listSmpUser == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		responseMessage.setExtend(listSmpUser);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/login")
	public ResponseMessage<SmpUserExtend> login(@RequestBody SmpUser user) {
		ResponseMessage<SmpUserExtend> responseMessage = new ResponseMessage<SmpUserExtend>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");
		
		SmpUser userRsult = smpUserMapper.selectByPrimaryKey(user.getUserName());
		
		
		if (userRsult == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("用户不存在");
		}else if(!userRsult.getUserPasswd().equals(user.getUserPasswd())) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("密码错误");
		}else {
			String accessToken = JWTUtil.sign(userRsult,  30*60*1000, jwtPassword,version);
			String freshToken = JWTUtil.sign(userRsult,  60*60*1000,jwtPassword,version);
			
			
			SmpUserExtend smpUserExtend = new SmpUserExtend();
			
			ObjectUtil.copyBeans(userRsult, smpUserExtend);
			
			smpUserExtend.setAccessToken(accessToken);
			smpUserExtend.setFreshToken(freshToken);
			
			smpUserExtend.setUserPasswd("");
			responseMessage.setExtend(smpUserExtend);
		}
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	
	/**
	 * 刷新token
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/tokenRefresh")
	public ResponseMessage<SmpUserExtend> login(@RequestBody SmpUserExtend user) {
		logger.info("刷新缓存");
		
		
		
		ResponseMessage<SmpUserExtend> responseMessage = new ResponseMessage<SmpUserExtend>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");
		
		//SmpUser userRsult = smpUserMapper.selectByPrimaryKey(user.getUserName());
		
		if(JWTUtil.verify(user.getFreshToken(), jwtPassword,version)) {
			String accessToken = JWTUtil.sign(user,  30*60*1000, jwtPassword,version);
			String freshToken = JWTUtil.sign(user,  60*60*1000,jwtPassword,version);
			user.setAccessToken(accessToken);
			user.setFreshToken(freshToken);
			responseMessage.setExtend(user);
		}else {
			responseMessage.setCode("0001");
			responseMessage.setMessage("token已过期");
		}
		
		
		return responseMessage;
	}
	
	
	

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectByPage")
	public ResponseMessage<PageInfo<SmpUserExtend>> selectByPage(@RequestBody PageRequest<SmpUser> pageRequest) {
		ResponseMessage<PageInfo<SmpUserExtend>> responseMessage = new ResponseMessage<PageInfo<SmpUserExtend>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		SmpUser smpUser = pageRequest.getBody();

		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<SmpUserExtend> listSmpUser = smpUserMapper.select(smpUser);
		if (listSmpUser == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		PageInfo<SmpUserExtend> pageInfo = new PageInfo<SmpUserExtend>(listSmpUser, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	
	

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/addUser")
	public ResponseMessage addUser(@RequestBody SmpUserExtend smpUser) {
		ResponseMessage<PageInfo<SmpUser>> responseMessage = new ResponseMessage<PageInfo<SmpUser>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户新增成功！");
		try {
			smpUser.setUserPasswd("670b14728ad9902aecba32e22fa4f6bd");
			smpUserMapper.insertSelective(smpUser);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户新增失败！");
		}
		
		SmpUserRole smpUserRole = new SmpUserRole();
		smpUserRole.setUserName(smpUser.getUserName());
		for (String roleId : smpUser.getUserRoles()) {
			smpUserRole.setRoleId(roleId);
			smpUserRoleMapper.insert(smpUserRole);
		}
		return responseMessage;
	}
	
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/updateUser")
	public ResponseMessage updateUser(@RequestBody SmpUserExtend smpUser) {
		ResponseMessage<PageInfo<SmpUser>> responseMessage = new ResponseMessage<PageInfo<SmpUser>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户修改成功！");
		try {
			smpUserMapper.updateByPrimaryKeySelective(smpUser);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户修改失败！");
		}

		
		smpUserRoleMapper.delByUserName(smpUser.getUserName());
		SmpUserRole smpUserRole = new SmpUserRole();
		smpUserRole.setUserName(smpUser.getUserName());
		for (String roleId : smpUser.getUserRoles()) {
			smpUserRole.setRoleId(roleId);
			smpUserRoleMapper.insert(smpUserRole);
		}
		
		return responseMessage;
	}


}
